# -*- coding: utf-8 -*-
# @Time    : 2024/2/20 20:06
# @Author  : micah
# @File    : 24.获取唯品会的商品信息.py
# @Software: PyCharm


import time
from random import randint
from pymongo import MongoClient
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class WpShop:
    def __init__(self):
        self.mongo_client = MongoClient()
        self.collection = self.mongo_client['py_spider']['wp_shop']

        # 创建浏览器配置对象
        options = webdriver.ChromeOptions()
        # 屏蔽图片
        prefs = {"profile.managed_default_content_settings.images": 2}
        options.add_experimental_option("prefs", prefs)

        self.browser = webdriver.Chrome(options=options)

    # 访问唯品会首页并搜索指定的商品
    def base(self):
        self.browser.get('https://vip.com')
        # 页面等待对象
        wait = WebDriverWait(self.browser, 10)
        el_input = wait.until(
            EC.presence_of_element_located((By.XPATH, "//input[@class='c-search-input  J-search-input']")))
        el_input.send_keys('jk制服')

        el_button = wait.until(
            EC.presence_of_element_located((By.XPATH, "//a[@class='c-search-button  J-search-button  J_fake_a']")))
        time.sleep(2)  # 让程序休眠2秒后点击对应的搜索按钮
        el_button.click()

        # 设置随机休眠时间让浏览器渲染新页面
        time.sleep(randint(1, 2))

    # 页面滚动
    def drop_down(self):
        for i in range(1, 13):
            js_code = f'document.documentElement.scrollTop = {i * 1000}'
            self.browser.execute_script(js_code)
            time.sleep(randint(1, 2))

    # 数据提取
    def parse_data(self):
        self.drop_down()
        div_list = self.browser.find_elements(
            By.XPATH,
            '//section[@id="J_searchCatList"]/div[@class="c-goods-item  J-goods-item c-goods-item--auto-width"]'
        )

        for temp in div_list:
            price = temp.find_element(By.XPATH,
                                      './/div[@class="c-goods-item__sale-price J-goods-item__sale-price"]').text
            title = temp.find_element(By.XPATH, './/div[2]/div[2]').text

            item = {
                'title': title,
                'price': price
            }
            print(item)
            self.save_mongo(item)
        self.next_page()

    # 数据保存
    def save_mongo(self, item):
        self.collection.insert_one(item)

    # 翻页
    def next_page(self):
        try:
            next_button = self.browser.find_element(By.XPATH, '//*[@id="J_nextPage_link"]')
            if next_button:
                next_button.click()
                self.parse_data()
            else:
                self.browser.close()
        except Exception as e:
            print('最后一页:', e)
            self.browser.quit()

    def main(self):
        self.base()
        self.parse_data()


if __name__ == '__main__':
    wp_shop = WpShop()
    wp_shop.main()
